<?php
namespace Home\Controller;
use Think\Controller;
class BalancepayController extends OrderController {

    /**
     * [delcart 清空购物车]
     * @return [type] [json]
     */
    private function clearcart($uid){
        // 删除用户购物车数据
        $Cart = M("Cart");
        $Cart->where("buyer_id = $uid")->delete();
    }


    public function index(){
        $uid = I('post.uid');
        $paypwd = I('post.paypwd');
        $ordersn = I('post.order_sn');
        if(!$ordersn)  returnApiError(1,'缺少订单sn号');
        if(!$uid)  returnApiError(1,'缺少uid');
        if(!$paypwd)  returnApiError(1,'请输入支付密码');

        $member = M('member');
        $padlog = M('pwd_log');
        $order = M('order');

        $member_data = $member->find($uid);
        if(!$member) returnApiError(1,'会员不存在');
        if(empty($member_data['member_paypwd'])){
            returnApiError(1,'请先设置支付密码');
        }
        if($member_data['member_paypwd'] != md5($paypwd)){
            //添加输入纪录
            $add['ip'] = $_SERVER["REMOTE_ADDR"];
            $add['add_time'] = date('Y-m-d');
            $add['member_id'] = $uid;
            $padlog->add($add);

            //输入次数限制
            $where['add_time'] = date('Y-m-d');
            $where['member_id'] = $uid;
            $where['ip'] = $_SERVER["REMOTE_ADDR"];
            $count = $padlog->where($where)->count();
            if($count > 3) {
                returnApiError(1,'密码输入超过限制');
            }
            returnApiError(1,'支付密码不正确');
        }
        $available = $member_data['available_predeposit'];
        $order_data = $order->where("pay_sn = $ordersn")->find();

        if(!$order_data) returnApiError(1,'订单号不存在');
        $money = $order_data['order_amount'];
        if($available < $money) returnApiError(1,'余额不足');


        $this->notify($ordersn,$money,$uid,$available,$money);
    }

    /**
     * 订单处理
     */
    public function notify($ordersn,$money,$uid,$available){
            //开启事务
            $obj = M();
            $obj->startTrans();

            //处理订单数据
            $order = M('order');
            $member = M('member');
            $order_pay = M('order_pay');
            $order_sn = $ordersn;
            $total_fee = $money;
            $callback = true;

            //查询是否重复提交
            $pay_where = array(
                'pay_sn' => $order_sn,
                'api_pay_state' => '1',
            );
            if($order_pay->where($pay_where)->find()){
               returnApiSuccess('已支付');
            }

            //修改订单状态
            $order_arr = array(
                'payment_time' => time(),
                'order_state' => 20,
                'payment_code' => 'balance',
                'pd_amount' => $total_fee,
            );
            $where['pay_sn'] = $order_sn;
            if(!$order->where($where)->save($order_arr)){
                $callback = false;
            }
            $total_fee_data = $order->where($where)->find();
            //修改支付状态
            $pay_arr = array(
                'api_pay_state' => 1,
            );
            if(!$order_pay->where("pay_sn = $order_sn")->save($pay_arr)){
                $callback = false;
            }

            //修改余额
            $map = array(
                'available_predeposit' => $available-$money,
            );
            if(!$member->where("member_id=$uid")->save($map)){
                $callback = false;
            }



            if($callback){
                $obj->commit();

                //下单赠积分
                $pointlog = D('PointsLog');  
                $insertarr["pl_memberid"] =$total_fee_data['buyer_id'];
                $insertarr["pl_membername"] =$total_fee_data['buyer_name'];
                $insertarr["order_sn"]= $total_fee_data['order_sn'];
                $insertarr["order_id"]=$total_fee_data["order_id"];
                $insertarr['orderprice']=$total_fee_data['order_amount'];
                $result = $pointlog->insert("order",$insertarr);
                $this->clearcart($uid);
                returnApiSuccess('支付成功');
            }else{
                $obj->rollback();
                returnApiError(1,'支付失败');
            }


        }




}